package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        String password = employee.getPassword();
        //对密码进行md5加密处理
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //根据用户名查询数据库中员工信息
        LambdaQueryWrapper<Employee> lmq = new LambdaQueryWrapper<>();
        lmq.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(lmq);
        //看是否查到员工姓名
        if (emp == null) {
            //没有,返回登录失败
            return R.error("登录失败");
        }
        //有,进行密码判断,看密码是否一致
        if (!emp.getPassword().equals(password)) {
            //不一致,返回登录失败
            return R.error("登录失败");
        }
        //一致判断账号是否被禁用
        if (emp.getStatus() == 0) {
            //禁用返回该账号已被禁用
            return R.error("账号已被禁用");
        }
        //没有被禁用,及登录成功,将员工id存入seesion,返回登录成功
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    //员工退出
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    //新增员工
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("新增员工信息:{}", employee.toString());
        //设置初始密码,并进行md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);
        return R.success("新增员工成功");
    }

    //分页查询员工
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("page = {},pageSize = {},name = {}", page, pageSize, name);
        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> lmq = new LambdaQueryWrapper<>();
        //添加条件
        lmq.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        //排序
        lmq.orderByDesc(Employee::getUpdateTime);
        //查询
        employeeService.page(pageInfo, lmq);
        return R.success(pageInfo);
    }

    //根据员工id修改员工状态
    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
        log.info(employee.toString());
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    //根据id查寻员工信息/编辑
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {
        log.info("根据id查询员工信息");
        Employee employee = employeeService.getById(id);
        if (employee != null) {
            return R.success(employee);
        }
        return R.error("无信息");
    }
}
